﻿namespace RMServiceRIA
{
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using System.Windows;
    using System;

    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class Home : Page
    {
        rService.Service1Client svc = new rService.Service1Client();
        /// <summary>
        /// Creates a new <see cref="Home"/> instance.
        /// </summary>
        public Home()
        {
            InitializeComponent();
            svc.getBuildingsCompleted += new System.EventHandler<rService.getBuildingsCompletedEventArgs>(svc_getBuildingsCompleted);
            this.Title = ApplicationStrings.HomePageTitle;
        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {   
        }

        void svc_getBuildingsCompleted(object sender, rService.getBuildingsCompletedEventArgs e)
        {
            cBox.SelectedIndex = -1;
            cBox.ItemsSource = e.Result;           
        }

        private void refreshBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            svc.getBuildingsAsync();
        }

        private void cBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            rService.ReserveMeBuilding selectedBuilding = (rService.ReserveMeBuilding)cb.SelectedItem;
            ((App)Application.Current).session.building = selectedBuilding;
            NavigationService.Navigate(new Uri("/Rooms", UriKind.RelativeOrAbsolute));
        }       
    }
}